﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using WCell.Constants;
using WCell.Constants.NPCs;
using WCell.Constants.Pets;
using WCell.RealmServer.Entities;
using WCell.Core;
using WCell.RealmServer.Database;

namespace WCell.RealmServer.NPCs.Pets
{
	/// <summary>
	/// Classic Hunter pets with talents and everything
	/// </summary>
	[ActiveRecord("Pets_Permanent", Access = PropertyAccess.Property)]
	public class PermanentPetRecord : PetRecordBase<PermanentPetRecord>, IPetRecord
	{
		public PermanentPetRecord()
		{
		}

		[Field("PetNumber", NotNull = true)]
		private int m_PetNumber;

		//public EntityId EntityId
		//{
		//    get;
		//    private set;
		//}

		public uint PetNumber
		{
			get { return (uint) m_PetNumber; }
			set { m_PetNumber = (int) value; }
		}

		[Property]
		public int Experience
		{
			get;
			set;
		}

		[Property]
		public int Level
		{
			get;
			set;
		}

		[Property]
		public DateTime? StabledSince
		{
			get;
			set;
		}

		[Property]
		public DateTime? LastTalentResetTime
		{
			get;
			set;
		}

		[Property]
		public int TalentResetPriceTier
		{
			get;
			set;
		}

		[Property(NotNull = true)]
		public int FreeTalentPoints
		{
			get;
			set;
		}

		/// <summary>
		/// TODO: Fix Spells
		/// </summary>
		public IList<PetTalentSpellRecord> Spells
		{
			get;
			set;
		}

		public override void SetupPet(NPC pet)
		{
			base.SetupPet(pet);

			pet.PetExperience = Experience;
			pet.Level = Level;
			pet.LastTalentResetTime = LastTalentResetTime;
			pet.FreeTalentPoints = FreeTalentPoints;
		}

		public override void UpdateRecord(NPC pet)
		{
			base.UpdateRecord(pet);

			Experience = pet.PetExperience;
			PetNumber = pet.PetNumber;
			Entry = pet.Entry;
			Level = pet.Level;
			LastTalentResetTime = pet.LastTalentResetTime;
			FreeTalentPoints = pet.FreeTalentPoints;
		}

		public static PermanentPetRecord[] LoadPermanentPetRecords(uint ownerId)
		{
			try
			{
				return FindAllByProperty("_OwnerLowId", (int) ownerId);
			}
			catch (Exception e)
			{
				RealmDBUtil.OnDBError(e);
				return FindAllByProperty("_OwnerLowId", (int)ownerId);
			}
		}
	}
}
